package net.minecraft.src;

import java.net.ConnectException;
import java.net.UnknownHostException;

import android.content.Context;
import android.content.Intent;

import lol.oxguy3.android.mcmc.NetMcmcHandler;
import lol.oxguy3.android.mcmc.ServerChatActivity;
import lol.oxguy3.android.mcmc.TickService;
import lol.oxguy3.android.mcmc.TickServiceConnection;
//import net.minecraft.client.Minecraft;
import lol.oxguy3.android.mcmc.ServerListActivity;

public class ThreadConnectToServer extends Thread
{
    /** A reference to the Minecraft object. */
    final Context mc;

    /** The IP address or domain used to connect. */
    final String ip;

    /** The port used to connect. */
    final int port;

    /** A reference to the GuiConnecting object. */
    final ServerChatActivity connectingGui;

    public ThreadConnectToServer(ServerChatActivity par1GuiConnecting,/* Minecraft par2Minecraft,*/Context par2Minecraft, String par3Str, int par4)
    {
        connectingGui = par1GuiConnecting;
        mc = par2Minecraft;
        ip = par3Str;
        port = par4;
    }

    public void run()
    {
        try
        {
            ServerChatActivity.setNetClientHandler(connectingGui, new NetMcmcHandler(connectingGui, ip, port));
            
            TickServiceConnection tsconn = new TickServiceConnection();
    		mc.bindService(new Intent(mc, TickService.class), tsconn, Context.BIND_AUTO_CREATE);
    		
        	TickService.bsService.startManagingPackets(ServerChatActivity.getNetClientHandler(connectingGui));

            if (ServerChatActivity.isCancelled(connectingGui))
            {
                return;
            }

            ServerChatActivity.getNetClientHandler(connectingGui).addToSendQueue(new Packet2Handshake(/*mc.session.username*/connectingGui.sprefs.getString("username", "Player"), ip, port));
        }
        catch (UnknownHostException unknownhostexception)
        {
            if (ServerChatActivity.isCancelled(connectingGui))
            {
                return;
            }

            Intent prefIntent = new Intent(mc, ServerListActivity.class);
            mc.startActivity(prefIntent);
            prefIntent.putExtra("errormessage", (new StringBuilder()).append("Unknown host '").append(ip).append("'").toString());
//            mc.displayGuiScreen(new GuiDisconnected("connect.failed", "disconnect.genericReason", new Object[]
//                    {
//                        (new StringBuilder()).append("Unknown host '").append(ip).append("'").toString()
//                    }));
        }
        catch (ConnectException connectexception)
        {
            if (ServerChatActivity.isCancelled(connectingGui))
            {
                return;
            }

            Intent prefIntent = new Intent(mc, ServerListActivity.class);
            mc.startActivity(prefIntent);
            prefIntent.putExtra("errormessage", connectexception.getMessage());
//            mc.displayGuiScreen(new GuiDisconnected("connect.failed", "disconnect.genericReason", new Object[]
//                    {
//                        connectexception.getMessage()
//                    }));
        }
        catch (Exception exception)
        {
            if (ServerChatActivity.isCancelled(connectingGui))
            {
                return;
            }

            exception.printStackTrace();
            Intent prefIntent = new Intent(mc, ServerListActivity.class);
            mc.startActivity(prefIntent);
            prefIntent.putExtra("errormessage", "Kicked: "+exception.getMessage());
//            mc.displayGuiScreen(new GuiDisconnected("connect.failed", "disconnect.genericReason", new Object[]
//                    {
//                        exception.toString()
//                    }));
        }
    }
}
